package com.panopset.gui;

import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import static com.panopset.Util.*;

import com.panopset.io.DocumentManager;

public class Widgets {
    private static Widgets SINGLETON;

    private Widgets() {}

    public static Widgets asWidgets() {
        if (SINGLETON == null) {
            SINGLETON = new Widgets();
        }
        return SINGLETON;
    }

    private static final String PWDKEY = "com.panopset.wg";

    /**
     * Prompt for password.
     * @param map Map that the calling program is responsible for
     * saving and retrieving.  If provided, a "remember" check box
     * will be added to the dialog.
     * @return
     */
    public String promptForPassword(DocumentManager documentManager) {
        JPasswordField jpw = new JPasswordField();
        JPanel p = new JPanel(new FlowLayout());
        p.add(jpw);
        TypicalDialog d = new TypicalDialog.Builder("Please enter your password")
        .centerPanel(p)
        .remember()
        .construct();
        d.getPanel().setLayout(new FlowLayout());
        jpw.setColumns(20);
        documentManager.load();
        String pwd = documentManager.get(PWDKEY);
        if (isPopulated(pwd)) {
            return pwd;
        }
        d.getFrame().setVisible(true);
        char[] pwa = jpw.getPassword();
        documentManager.save();
        return pwa == null ? "" : new String(jpw.getPassword());
    }

}
